<?
/**
 * Validate a record
 *
 * @params    record        Values to insert.
 */

$fields = $this->fields;

$mode = "_validate";
foreach ($fields as $k=>$f) {
    if (isset($f[$mode]))
    foreach ($f[$mode] as $kk=>$vv)
        $fields[$k][$kk]=$vv;
    }

$oe = array();
foreach ($fields as $k=>$f) {
    @$oe[$k]=$params["record"][$k];
    }

$e = $oe;

unset($e[$this->key]);
if (isset($this->id)) {
    $_e = $this->db->query1("select * from `{$this->table}` where {$this->key} = ".$this->db->sqlint($this->id));
    $e[$this->key]=$this->id;
    }

$errors = 0;
$e = $oe;
foreach ($fields as $k=>$f) {
    $p = $f;
    $v = $e[$k];
    $p["name"]=$k;
    $p["value"]=$v;
    $p["record"] = $e;
    @$p["_record"] = $_e;

    $error = "";

    if (!$error && isset($f["required"]) && $f["required"] == 1) {
        if ($v == "") {
            $error = "{$f["title"]} is a required field.";
            }
        }

    if (!$error && isset($f["regex"])) {
        if ($v != "" && !preg_match("/{$f["regex"]}/",$v)) {
            $error = $f["regex_error"];
            }
        }

    if (!$error) {
        $type = $f["type"];
        $inc = isset($f["inc"])?$f["inc"]:"fields3/{$type}_validate.php";
        if ($this->__exists($inc)) {
            $_error = $this->__inc($inc,$p);
            if (isset($_error) && $_error != 1) {
                $error = $_error;
                }
            }
        }

    if ($error) {
        $this->fields[$k]["error"] = $error;
        $errors++;
        }
    }

return !$errors;
?>
